package com.atguigu.mybatis.mapper;

import com.atguigu.mybatis.bean.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Smexy on 2022/11/9
 *
 *      增删改需要提交事务！
 *
 */
public class EmployeeMapperTest2
{

    private  SqlSessionFactory sqlSessionFactory;


    {
        String resource = "config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }


    /*
        ### Cause: org.apache.ibatis.binding.BindingException(sql中的占位符无法填充，或Mybatis找不到sql):
        Parameter 'a' not found. Available parameters are [arg1, arg0, param1, param2]

        mapper.getEmpByIdAndName(2, "jackma")
                默认把多个参数放入一个Map集合
                        { arg1: jackma ,arg0 = 2 , param1 =2,param2=jackma }

        where id = #{a} and name = #{b}
            #{xxx}: xxx要key的名字

     */
    @Test
    public void getTest() {

        SqlSession session = sqlSessionFactory.openSession();

        try {
            EmployeeMapper1 mapper = session.getMapper(EmployeeMapper1.class);

            //System.out.println(mapper.getEmpById2(2));
            System.out.println(mapper.getEmpById4("employee"));
            //System.out.println(mapper.getEmpByIdAndName(2, "jackma"));

        } finally {
            session.close();
        }
    }
}